<?php

class ControllerApiCategory extends Controller
{
    //分类同步
    public function sync()
    {
        $this->load->language('catalog/category');
        $json = array();
        $check = $this->check->checkToken();
        if (!$check) {
            $json['code'] = 401;
            $json['error']['warning'] = $this->language->get('error_permission');
        } else {
            if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
                $post = json_decode(file_get_contents("php://input"),true);
                if (!isset($post['categories'])) {
                    $json['code'] = 400;
                    $json['error']['warning'] = sprintf($this->language->get('categories required'), '');
                }
                if (!isset($json['error'])) {
                    $data = $post['categories'];
                    if($data){
                        $this->load->model('catalog/category');
                        $this->model_catalog_category->truncateCategory();
                        foreach ($data as $i=>$item){
                            $post["category_id"] = !empty($item['id'])?intval($item['id']):null;
                            $post["parent_id"] = !empty($item['pid'])?intval($item['pid']):0;
                            $post['column'] = 1;
                            $post['category_store'] = [0];
                            $post['status'] =  isset($item['status'])?intval($item['status']):1;
                            $post['sort_order'] =  isset($item['sort'])?intval($item['sort']):0;
                            $lans = $this->getLanguages();
                            foreach ($lans as $lan){
                                $language_id = $lan['language_id'];
                                $language_code = $lan['code'];
                                $name = $item['name'][$language_code]??'';
                                if(!$name) continue;
                                $post["category_description"][$language_id]["name"] = $name;
                                $post["category_description"][$language_id]["meta_title"] = $name;
                                $post["category_description"][$language_id]["meta_description"] = "";
                                $post["category_description"][$language_id]["description"] = "";
                                $post["category_description"][$language_id]["meta_keyword"] = "";
                                $seo[$language_id] = $name . $lan['code'];
                            }
                            $post["category_seo_url"][0] = $seo;
                            if(!empty($item['icon'])){
                                $post["image"] = $item['icon'];
                            }
                            $this->model_catalog_category->addCategory($post);
                        }
                        $json['code'] = 200;
                    }else{
                        $json['code'] = 201;
                    }
                    $json['success'] = $this->language->get('text_success');

                }
            }
        }
        if (isset($this->request->server['HTTP_ORIGIN'])) {
            $this->response->addHeader('Access-Control-Allow-Origin: ' . $this->request->server['HTTP_ORIGIN']);
            $this->response->addHeader('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
            $this->response->addHeader('Access-Control-Max-Age: 1000');
            $this->response->addHeader('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
        }
        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($json));
    }

    protected function getLanguages() {
        $query = $this->db->query( "SELECT * FROM `".DB_PREFIX."language` WHERE `status`=1 ORDER BY `code`" );
        return $query->rows;
    }

}